package com.sina.shop.manage.controller.card;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.card.Card;
import com.sina.shop.manage.service.card.CardManageService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-11-10:39
 */
@Controller
@RequestMapping("cardManage")
public class CardManageController extends BaseController {

    @Autowired
    @Qualifier("cardManageService")
    private CardManageService cardManageService;

    @RequestMapping(value = "getCardList", method = RequestMethod.GET)
    public ModelAndView getCardList(@Valid Card card) {
        LOG.info("card:{}", card);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = cardManageService.getCardList(card, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "deleteCard", method = RequestMethod.POST)
    public ModelAndView deleteCard(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            cardManageService.deleteCard(id);
            result.put(CODE, OK);
            result.put(MSG, "删除购物记录成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "updateCard", method = RequestMethod.POST)
    public ModelAndView updateCard(@Valid Card card) {

        LOG.info("card:{}", card);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            cardManageService.updateCard(card);
            result.put(CODE, OK);
            result.put(MSG, "更新购物记录成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "saveCard", method = RequestMethod.POST)
    public ModelAndView saveCard(@Valid Card card) {

        LOG.info("card:{}", card);

        /*创建时间、修改时间、创建人*/
        card.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            cardManageService.saveCard(card);
            result.put(CODE, OK);
            result.put(MSG, "新增购物记录成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getCardListToHtml", method = RequestMethod.GET)
    private ModelAndView getCardListToHtml() {

        ModelAndView mv = new ModelAndView("card/cardList");

        return mv;
    }
}
